package com.ruoyi.project.zxmes.dv.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.zxmes.dv.domain.DvMachinery;
import com.ruoyi.project.zxmes.dv.domain.DvMachineryType;
import com.ruoyi.project.zxmes.dv.domain.TreeSelectMes;
import com.ruoyi.project.zxmes.dv.mapper.DvMachineryMapper;
import com.ruoyi.project.zxmes.dv.service.IDvMachineryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 设备Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-05-08
 */
@Service
@AllArgsConstructor
public class DvMachineryServiceImpl implements IDvMachineryService {

    private DvMachineryMapper dvMachineryMapper;

    /**
     * 查询设备
     *
     * @param machineryId 设备主键
     * @return 设备
     */
    @Override
    public DvMachinery selectDvMachineryByMachineryId(Long machineryId) {
        return dvMachineryMapper.selectDvMachineryByMachineryId(machineryId);
    }

    /**
     * 查询设备列表
     *
     * @param dvMachinery 设备
     * @return 设备
     */
    @Override
    public List<DvMachinery> selectDvMachineryList(DvMachinery dvMachinery) {
        return dvMachineryMapper.selectDvMachineryList(dvMachinery);
    }

    /**
     * 新增设备
     *
     * @param dvMachinery 设备
     * @return 结果
     */
    @Override
    public int insertDvMachinery(DvMachinery dvMachinery) {
        dvMachinery.setCreateTime(DateUtils.getNowDate());
        return dvMachineryMapper.insertDvMachinery(dvMachinery);
    }

    /**
     * 修改设备
     *
     * @param dvMachinery 设备
     * @return 结果
     */
    @Override
    public int updateDvMachinery(DvMachinery dvMachinery) {
        dvMachinery.setUpdateTime(DateUtils.getNowDate());
        return dvMachineryMapper.updateDvMachinery(dvMachinery);
    }

    /**
     * 批量删除设备
     *
     * @param machineryIds 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDvMachineryByMachineryIds(Long[] machineryIds) {
        return dvMachineryMapper.deleteDvMachineryByMachineryIds(machineryIds);
    }

    /**
     * 删除设备信息
     *
     * @param machineryId 设备主键
     * @return 结果
     */
    @Override
    public int deleteDvMachineryByMachineryId(Long machineryId) {
        return dvMachineryMapper.deleteDvMachineryByMachineryId(machineryId);
    }

    @Override
    public List<TreeSelectMes> buildTreeSelect(List<DvMachineryType> list) {
        List<DvMachineryType> dvMachineryTypes = list.stream().filter(a -> a.getParentTypeId() == 0).map(a -> {
            a.setChildren(findParent(list, a));
            return a;
        }).collect(Collectors.toList());
        return dvMachineryTypes.stream().map(TreeSelectMes::new).collect(Collectors.toList());
    }

    private List<DvMachineryType> findParent(List<DvMachineryType> list, DvMachineryType dvMachineryType) {
        List<DvMachineryType> collect = list.stream().filter(a -> a.getParentTypeId().equals(dvMachineryType.getMachineryTypeId()))
                .map(a -> {
                    a.setChildren(findParent(list, a));
                    return a;
                }).collect(Collectors.toList());
        return collect;
    }
}
